package com.itic.appbase.applications.sys.code.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.applications.cacheutils.CodeCache;
import com.itic.appbase.applications.sys.code.persistence.dao.CodeDetailDao;
import com.itic.appbase.applications.sys.code.persistence.model.CodeDetail;
import com.itic.appbase.applications.sys.code.persistence.model.CodeDetailExample;
import com.itic.appbase.applications.sys.code.persistence.model.CodeDetailExample.Criteria;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.PropertyGetter;
import com.itic.appbase.framework.utils.StringHelper;

@Service
@Transactional
public class CodeDetailService
        extends CrudService<CodeDetailDao, CodeDetail, CodeDetailExample> {
    @Autowired
    private CodeDetailDao codeDetailDao;

    /**
     * 覆盖父类，增加清除缓存
     * 
     * @param codeDetail
     * @param user
     */
    @Override
    public String add(CodeDetail codeDetail) {
        String result = super.add(codeDetail);
        // 清除缓存
        CodeCache.remove(codeDetail.getType());
        return result;
    }

    /**
     * 覆盖父类，增加清除缓存
     * 
     * @param codeDetail
     */
    @Override
    public String edit(CodeDetail codeDetail) {
        String result = super.edit(codeDetail);
        // 删除缓存
        CodeCache.remove(codeDetail.getType());
        return result;
    }

    /**
     * 获取级联代码详情
     * 
     * @param parentId
     * @return List<CodeDetail>
     */
    public List<CodeDetail> selectCodeDetail(String parentId) {
        if (StringHelper.isNotBlank(parentId)) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("dbTrue", DBConst.TRUE);// 可用的代码表
            map.put("parentId", parentId);// parentId
            String isMysql = PropertyGetter.getString("app.db.type");
            if( StringHelper.isNotBlank(isMysql)&&"mysql".equals(isMysql) ){
            	return codeDetailDao.selectMysqlCodeDetail(map);
            }else{
            	return codeDetailDao.selectCodeDetail(map);
            }
           
        }
        return new ArrayList<CodeDetail>();
    }

    
    /**
     * 删除代码详情表信息
     * 
     * @param codeDetail
     * @param codeDetailExample
     * @param idList
     *            父id
     * @param type
     * @return
     */
    public String deleteCodeDetail(CodeDetail codeDetail, CodeDetailExample codeDetailExample, List<String> idList) {
        String result = FAILED;
        // 删除代码详情表中的父节点数据
        int updateCount = codeDetailDao.updateByExampleSelective(codeDetail, codeDetailExample);
        if (updateCount > 0) {
            result = SUCCESS;
            // 删除代码详情表中子节点数据
            if( null != idList && idList.size() > 0){
            	String isMysql = PropertyGetter.getString("app.db.type");
            	if( StringHelper.isNotBlank(isMysql)&&"mysql".equals(isMysql) ){
            		CodeDetailExample example = new CodeDetailExample();
            		Criteria createCriteria = example.createCriteria();
            		createCriteria.andParentIdIn(idList);
            		int deleteCount = codeDetailDao.deleteByExample(example);
            		if (deleteCount < 0) {
                        result = FAILED;
                    }
            	}else{
            		Map<String, Object> map = new HashMap<String, Object>();
                    map.put("dbFalse", DBConst.FALSE);// 设置代码不可用
                    map.put("dbTrue", DBConst.TRUE);// 查询可用的代码表
                    map.put("idList", idList);// 角色id组成的list集合
                    map.put("updateTime", new Date());// 可用的代码表

                    int deleteCount = codeDetailDao.deleteCodeDetail(map);
                    if (deleteCount < 0) {
                        result = FAILED;
                    }
            	}
            }
            
            // 删除缓存
            CodeCache.remove(codeDetail.getType());
        }
        return result;
    }
}